import {
  Body,
  Controller,
  Get,
  Param,
  ParseIntPipe,
  Post,
  Query,
  Request,
  UseGuards,
} from '@nestjs/common';
import { ArticleService } from './article.service';
import {
  CreateArticleDto,
  DeleteArticleDto,
  QueryArticleDto,
  QueryListDto,
  UpdateArticleDto,
  UpdateArticleStatusDto,
} from '@/modules/article/dto/dto';
import { AuthGuard } from '@nestjs/passport';

@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  // 获取数据统计
  @Get('/total')
  async articlesTotal() {
    return await this.articleService.articlesTotal();
  }

  // 按条件对象查询文章列表
  @UseGuards(AuthGuard('jwt'))
  @Get('/query')
  async articleQuery(@Query() data: QueryListDto) {
    return await this.articleService.articleQuery(data);
  }

  // 获取文章列表
  @Get('/list')
  async articleList(@Query() data: QueryArticleDto) {
    return await this.articleService.articleList(data);
  }

  // 获取用户的文章列表
  @UseGuards(AuthGuard('jwt'))
  @Get('/my')
  async articleMy(@Query() data: QueryArticleDto, @Request() req: any) {
    return await this.articleService.articleMy(data, req.user.userId);
  }

  // 获取用户的草稿文章列表
  @UseGuards(AuthGuard('jwt'))
  @Get('/draft')
  async articleDraft(@Query() data: QueryArticleDto, @Request() req: any) {
    return await this.articleService.articleDraft(data, req.user.userId);
  }

  // 按id获取单篇文章
  // 添加 view 参数增加阅读量
  @Get('/:id')
  async articleFindOne(
    @Param('id', ParseIntPipe) id: number,
    @Query('view') view: boolean = false,
  ) {
    return await this.articleService.articleFindOne(id, view);
  }

  // 创建文章
  @UseGuards(AuthGuard('jwt'))
  @Post('/create')
  async articleCreate(@Body() data: CreateArticleDto, @Request() req: any) {
    return await this.articleService.articleCreate(data, req.user.userId);
  }

  // 更新文章
  @UseGuards(AuthGuard('jwt'))
  @Post('/update/:id')
  async articleUpdate(@Param('id', ParseIntPipe) id: number, @Body() data: UpdateArticleDto) {
    return await this.articleService.articleUpdate(id, data);
  }

  // 更新文章状态
  @UseGuards(AuthGuard('jwt'))
  @Post('/update/status/:id')
  async articleUpdateStatus(
    @Param('id', ParseIntPipe) id: number,
    @Body() data: UpdateArticleStatusDto,
  ) {
    return await this.articleService.articleUpdateStatus(id, data);
  }

  // 文章点赞
  @UseGuards(AuthGuard('jwt'))
  @Post('/like/:id')
  async articleLike(@Param('id', ParseIntPipe) id: number) {
    return await this.articleService.articleLike(id);
  }

  // 批量删除文章
  @UseGuards(AuthGuard('jwt'))
  @Post('/remove')
  async articleRemove(@Body() data: DeleteArticleDto) {
    return await this.articleService.articleRemove(data.ids);
  }

  // 按id删除文章
  @UseGuards(AuthGuard('jwt'))
  @Post('/remove/:id')
  async articlesRemoveOne(@Param('id', ParseIntPipe) id: number) {
    return await this.articleService.articleRemoveOne(id);
  }
}
